package com.wanlian.router.ui.manage;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import com.smart.android.ui.BaseActivity;
import com.wanlian.router.R;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hyu on 2018/9/5.
 * Email: fvaryu@qq.com
 */
public class DeviceDetailActivity extends BaseActivity {
    private static final float MAX_PERCENT = 5 * 1024 / 100.f;

    @BindView(R.id.tv_speed_limit)
    TextView tv_speed_limit;
    private boolean isShow = true;
    @BindView(R.id.ll_limit)
    View ll_limit;

    @BindView(R.id.seek_down)
    SeekBar mSeekBarDown;
    @BindView(R.id.seek_up)
    SeekBar mSeekBarUp;
    @BindView(R.id.tv_speed_up)
    TextView tv_speed_up;
    @BindView(R.id.tv_speed_down)
    TextView tv_speed_down;
    @BindView(R.id.tv_ip)
    TextView tv_ip;
    @BindView(R.id.tv_mac)
    TextView tv_mac;
    @BindView(R.id.tv_current_speed)
    TextView tv_current_speed;

    @Override
    protected int layout() {
        return R.layout.activity_device_detail;
    }

    @Override
    public void initUI() {
        super.initUI();
        setTitle("设备详情");
    }

    @Override
    public void initData() {
        super.initData();
        speedLimit();

        SpannableString ss = new SpannableString("MAC地址  F8:DA:0C:13:40:8D");
        ss.setSpan(new ForegroundColorSpan(getColorById(R.color.color_99)), 7, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new RelativeSizeSpan(0.8f), 7, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_mac.setText(ss);

        ss = new SpannableString("IP地址       F8:DA:0C:13:40:8D");
        ss.setSpan(new ForegroundColorSpan(getColorById(R.color.color_99)), 11, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new RelativeSizeSpan(0.8f), 11, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_ip.setText(ss);


        ss = new SpannableString("上传速度：128Kb/s   下载速度：128Kb/s");
        ss.setSpan(new RelativeSizeSpan(1.2f), 5, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new RelativeSizeSpan(1.2f), 20, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_current_speed.setText(ss);


        mSeekBarUp.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                format(progress, tv_speed_up);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mSeekBarDown.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                format(progress, tv_speed_down);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void format(int progress, TextView tv_speed) {
        if (progress == 0) {
            tv_speed.setText("不限速");
            return;
        }
        if (progress > 20) {
            tv_speed.setText(String.format(Locale.getDefault(), "%.2fMb/s", progress * MAX_PERCENT / 1024));
        } else {
            tv_speed.setText(String.format(Locale.getDefault(), "%.0fKb/s", progress * MAX_PERCENT));
        }
    }

    @OnClick(R.id.tv_speed_limit)
    void speedLimit() {
        isShow = !isShow;
        tv_speed_limit.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,
                isShow ? R.drawable.up : R.drawable.down,0);
        ll_limit.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }
}
